package kd.bos.openapi.demo.auth.signauth;

import com.alibaba.fastjson.JSON;
import kd.bos.openapi.demo.auth.oauth2.TokenDemoTest;
import kd.bos.openapi.demo.auth.signauth.dto.BosKeyInfoDto;
import kd.bos.openapi.demo.auth.signauth.dto.ThirdAppInfoDto;
import kd.bos.openapi.common.result.CustomApiResult;
import kd.bos.openapi.sdk.auth.signauth.OpenApiSignAuthUtil;
import kd.bos.openapi.sdk.auth.signauth.SignAuthParamDto;
import kd.bos.openapi.sdk.dto.OrderDto;
import kd.bos.openapi.sdk.request.RequestOperatorDto;
import kd.bos.openapi.sdk.request.RequestQueryDto;
import kd.bos.openapi.sdk.response.query.QueryDataDto;
import kd.bos.openapi.sdk.response.save.SaveDataDto;
import kd.bos.openapi.sdk.util.PropertyUtil;
import kd.bos.openapi.security.model.EncryptionEnum;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @author johnz
 * @Deacription 签名认证测试用例
 * @date 2023年10月20日
 */
public class SignAuthTest {
    private final static Logger log = Logger.getLogger(SignAuthTest.class.getName());
    //苍穹OpenAPI第三方应用-AES/SM4数据加密策略-开放平台证书
    public static final String bosPublickBase64Key = PropertyUtil.getProperty("signAuth.bosPublickBase64Key");
    //苍穹OpenAPI第三方应用-签名认证策略-Secret key
    public static final String openApiSign = PropertyUtil.getProperty("signAuth.openApiSign");

    //苍穹OpenAPI第三方应用-签名认证策略-认证密钥
    public static final String appSecret =  PropertyUtil.getProperty("signAuth.appSecret");
    // 苍穹OpenAPI调用接口的URL前缀
    public static final String urlPrefix = PropertyUtil.getProperty("bos.env.cosmic");

    public static void main(String[] args) throws Exception {
        testSignOperatorFindApi();
        // testSignOperatorSaveApi();
        // testSignCustomApi();
    }

    /**
     * 测试操作类API 查询操作 签名认证
     */
    public static void testSignOperatorFindApi() throws Exception {
        log.info("------------------------testSignOperatorFindApi start");
        // 接口地址
        String url = "kapi/v2/open/openapi_unittest/testSignAuth";
        // 请求模型封装, 这里的OrderDto对象中的属性对应API接口界面的入参定义
        RequestQueryDto<OrderDto> openApiRequest = new RequestQueryDto<>();
        OrderDto orderDto = new OrderDto();
        orderDto.setBillno("unittest-00000002");
        openApiRequest.setData(orderDto);
        openApiRequest.setPageNo(1);
        openApiRequest.setPageSize(20);
        // 构造签名认证需要的参数
        SignAuthParamDto params = new SignAuthParamDto(bosPublickBase64Key, appSecret, openApiSign, (long) EncryptionEnum.AES.getId());
        // 打印请求参数
        params.setPrint(true);
        // 发送请求
        CustomApiResult<QueryDataDto> result = OpenApiSignAuthUtil.sendSignApiRequest(JSON.toJSONString(openApiRequest), urlPrefix + url, QueryDataDto.class, params);
        // 打印结果
        log.info("------------------------response--------------------------------------------");
        log.info(JSON.toJSONString(result, true));
        log.info("------------------------testSignOperatorFindApi end");
    }

    /**
     * 测试操作类API 保存操作 签名认证
     */
    public static void testSignOperatorSaveApi() throws Exception {
        log.info("------------------------testSignOperatorSaveApi start");
        // 接口地址
        String url = "kapi/v2/open/openapi_unittest/signAuth_save";
        // 请求模型封装, 这里的OrderDto对象中的属性对应API接口界面的入参定义
        RequestOperatorDto<OrderDto> openApiRequest = new RequestOperatorDto<>();
        OrderDto orderDto = new OrderDto();
        orderDto.setBillno("unittest-00000003");
        openApiRequest.setData(orderDto);
        // 构造签名认证需要的参数
        SignAuthParamDto params = new SignAuthParamDto(bosPublickBase64Key, appSecret, openApiSign, (long) EncryptionEnum.AES.getId());
        // 发送请求
        CustomApiResult<SaveDataDto> result = OpenApiSignAuthUtil.sendSignApiRequest(JSON.toJSONString(openApiRequest), urlPrefix + url, SaveDataDto.class, params);
        // 打印结果
        log.info("------------------------response--------------------------------------------");
        log.info(JSON.toJSONString(result, true));
        log.info("------------------------testSignOperatorSaveApi end");
    }

    /**
     * 测试自定义API 签名认证
     */
    public static void testSignCustomApi() throws Exception {
        log.info("------------------------testSignCustomApi start");
        // 接口地址
        String url = "kapi/v2/open/openapi/gen/getBosRsaPublicKey";
        // 请求模型封装，这里的thirdAppInfoDto对象中的属性对应接口界面入参的定义，自定义API接口入参需要用map将构建的对象包起来，名称为ThirdAppInfoDto对象名称小驼峰形式thirdAppInfoDto
        ThirdAppInfoDto thirdAppInfoDto = new ThirdAppInfoDto();
        thirdAppInfoDto.setThirdId("1533186420334907392");
        thirdAppInfoDto.setAccountId("1173910536060928000");
        Map<String, ThirdAppInfoDto> requestParams = new HashMap<>();
        requestParams.put("thirdAppInfoDto", thirdAppInfoDto);
        // 构造签名认证需要的参数
        SignAuthParamDto params = new SignAuthParamDto(bosPublickBase64Key, appSecret, openApiSign, (long) EncryptionEnum.AES.getId());
        // 发送请求
        CustomApiResult<BosKeyInfoDto> result = OpenApiSignAuthUtil.sendSignApiRequest(JSON.toJSONString(thirdAppInfoDto), urlPrefix + url, BosKeyInfoDto.class, params);
        // 打印结果
        log.info("------------------------response--------------------------------------------");
        log.info(JSON.toJSONString(result, true));
        log.info("------------------------testSignCustomApi end");
    }


}